﻿using ERP.Inventory.Api.Read.Application.Command.库存管理;
using ERP.Inventory.Api.Read.DTO;
using ERP.Inventory.Domain.库存管理;
using ERP.Inventory.Errorcode;
using ERP.Inventory.Infastructure.Interface;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace ERP.Inventory.Api.Read.Application.Handler.库存管理
{
    public class GetPutHandler : IRequestHandler<GetPutCommand, ApiPaging<PutDTO>>
    {
        private readonly IBaseRepository<PutModel> putRepo;
        private readonly IBaseRepository<CheckandShopModel> checkshopRepo;
        private readonly IBaseRepository<ShopModel> shopRepo;
        private readonly IBaseRepository<StoreModel> storeRepo;

        public GetPutHandler(IBaseRepository<PutModel> putRepo, IBaseRepository<CheckandShopModel> checkshopRepo, IBaseRepository<ShopModel> shopRepo, IBaseRepository<StoreModel> storeRepo)
        {
            this.putRepo = putRepo;
            this.checkshopRepo = checkshopRepo;
            this.shopRepo = shopRepo;
            this.storeRepo = storeRepo;
        }
        public Task<ApiPaging<PutDTO>> Handle(GetPutCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiPaging<PutDTO>();

            result.Code = ApiEnum.Ok;
            result.Message = "获取成功";
            var list = (from a in putRepo.GetAll().AsNoTracking().Where(x=>x.IsIntOrOut==request.IsIntOrOut)
                        join b in storeRepo.GetAll().AsNoTracking()
                        on a.StoreId equals b.Id
                        select new PutDTO
                        {
                            Id = a.Id,
                            InventoryId = a.InventoryId,
                            InStoreNo = a.InStoreNo,
                            GoodsIds = a.GoodsIds,
                            IntOutStoreTime = a.IntOutStoreTime,
                            StoreId = a.StoreId,
                            Count = a.Count,
                            Reson = a.Reson,
                            IsIntOrOut = a.IsIntOrOut,
                            AttnId = a.AttnId,
                            AttnName = a.AttnName,
                            CreateUserName = a.CreateUserName,
                            StoreName = b.StoreName,
                            ShopName = ""
                        }).ToList();
            ApiPaging<PutDTO> paging = new ApiPaging<PutDTO>();
            paging.totalcount = list.Count();
            foreach (var item in list)
            {
                var ids = item.GoodsIds.Split(',').Select(x => Convert.ToInt32(x));
                var models = shopRepo.GetAll().Where(x => ids.Contains(x.Id)).Select(x => x.ShopName);
                item.ShopName = string.Join("、", models);
            }
            list = list.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Data = list;
            result.totalcount = paging.totalcount;
            return Task.FromResult(result);
        }
    }
}
